package flc.ast.fragment;

import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.calculator.tax.model.TaxCalRetBean;
import com.stark.calculator.tax.util.IncomeType;
import com.stark.calculator.tax.util.TaxUtil;

import flc.ast.R;
import flc.ast.activity.IncomeKind2RetActivity;
import flc.ast.databinding.FragmentIncomeKind3Binding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.Str2NumUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IncomeKind3Fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IncomeKind3Fragment extends BaseNoModelFragment<FragmentIncomeKind3Binding> {

    @Override
    protected int onCreate() {
        return R.layout.fragment_income_kind3;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivStartCal.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivStartCal:
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    /**
     * 计算结果
     */
    private void calculate() {
        String text = mDataBinding.etIncomeKind3Sqsr.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            mDataBinding.etIncomeKind3Sqsr.requestFocus();
            ToastUtils.showShort(R.string.input_valid_amount_tip);
            return;
        }
        float value = Str2NumUtil.parse(text);
        if (value <= 0) {
            mDataBinding.etIncomeKind3Sqsr.requestFocus();
            ToastUtils.showShort(R.string.input_valid_amount_tip);
            return;
        }
        TaxCalRetBean retBean = TaxUtil.calculate(value, IncomeType.REMUNERATION);

        //跳去结果页面
        goCalRetActivity(retBean);
    }

    protected void goCalRetActivity(TaxCalRetBean retBean) {
        IncomeKind2RetActivity.start(getContext(), retBean);
    }

}

